package com.atguigu.func;

import com.atguigu.utils.KeyWordUtil;
import org.apache.flink.table.annotation.DataTypeHint;
import org.apache.flink.table.annotation.FunctionHint;
import org.apache.flink.table.functions.TableFunction;
import org.apache.flink.types.Row;

import java.io.IOException;
import java.util.List;

//参考https://nightlies.apache.org/flink/flink-docs-release-1.13/docs/dev/table/functions/udfs/#table-functions自定义UDTF函数
//自定义UDTF：切分单词
@FunctionHint(output = @DataTypeHint("ROW<word STRING>"))//设置输出的字段别名和字段类型，我们把单词切分切成一个列，所以只用返回一个字段
public class SplitFunction extends TableFunction<Row> {
    public void eval(String keyWord){
        try {
            List<String> words = KeyWordUtil.splitKeyWord(keyWord);
            for (String word : words) {
                //能切的了的话把keyword切成一个一个单词输出出去
                collect(Row.of(word));
            }
        } catch (IOException e) {
            //切不了的话把keyword当作整体输出出去
            collect(Row.of(keyWord));
        }
    }
}
